package cn.itcast.nems.studentrecord.service.impl;

import cn.itcast.nems.order.api.OrderApi;
import cn.itcast.nems.studentrecord.constant.ReferenceTypeEnum;
import cn.itcast.nems.studentrecord.converter.StudentRecordReferenceConverter;
import cn.itcast.nems.studentrecord.dao.entity.StudentRecord;
import cn.itcast.nems.studentrecord.dao.entity.StudentRecordTransaction;
import cn.itcast.nems.studentrecord.dto.change.StudentRecordChangeCheckOriginDTO;
import cn.itcast.nems.studentrecord.dto.change.StudentRecordJournalDTO;
import cn.itcast.nems.studentrecord.dto.change.StudentRecordJournalFinalDTO;
import cn.itcast.nems.studentrecord.dto.change.StudentRecordTransactionChangeCheckDTO;
import cn.itcast.nems.studentrecord.dto.studentrecord.StudentRecordReferenceDTO;
import cn.itcast.nems.studentrecord.service.StudentRecordFinalService;
import cn.itcast.nems.studentrecord.service.settlement.SettlementCalculatorFinal;
import cn.itcast.nems.studentrecord.service.settlement.SettlementResult;
import cn.itcast.nems.studentrecord.service.studentrecord.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author liyong
 * create: 2023-12-07 21:04:59
 **/
@Service
public class StudentRecordFinalServiceImpl extends StudentRecordChangeServiceImpl implements StudentRecordFinalService {

    public StudentRecordFinalServiceImpl(StudentRecordTransactionService studentRecordTransactionService,
                                         StudentRecordService studentRecordService,
                                         StudentRecordCreateService studentRecordCreateService,
                                         StudentRecordMessageService studentRecordMessageService,
                                         StudentRecordReceivableService studentRecordReceivableService,
                                         OrderApi orderApi) {
        super(studentRecordService,
                studentRecordCreateService,
                studentRecordTransactionService,
                studentRecordMessageService,
                studentRecordReceivableService,
                orderApi);
    }

    @Override
    @Transactional
    public void finish(StudentRecordJournalFinalDTO studentRecordJournalFinal) {
        SettlementCalculatorFinal calculator = new SettlementCalculatorFinal(studentRecordJournalFinal.createSettlementResult());
        logger.info("处理结课: {}", studentRecordJournalFinal);
        final SettlementResult settlementResult = super.studentRecordService.studentRecordSettlement(
                studentRecordJournalFinal.getStudentRecordId(),
                studentRecordJournalFinal.getSettlementDate(),
                calculator);
        logger.info("{}结课结算结果: {}", studentRecordJournalFinal.getStudentRecordId(), settlementResult);
        StudentRecordJournalDTO joutnalDTO = convertJournalDTO(studentRecordJournalFinal, settlementResult);
        StudentRecord sr = this.studentRecordService.getById(studentRecordJournalFinal.getStudentRecordId());
        StudentRecordReferenceDTO dto = StudentRecordReferenceConverter.origin(sr, joutnalDTO);
        logger.info("创建结课流转记录，学籍ID: '{}' {}", sr.getId(), dto);
        super.createStudentRecordTransaction(dto, settlementResult, StudentRecordTransaction.TYPE_OUT);
        super.studentRecordService.unlock(studentRecordJournalFinal.getStudentRecordId());
    }

    @Override
    StudentRecordChangeChecker createStudentRecordChangeChecker(StudentRecordChangeCheckOriginDTO studentRecordOrigin, List<StudentRecordTransactionChangeCheckDTO> transactions) {
        return new StudentRecordChangeFinalChecker(studentRecordOrigin, transactions);
    }

    private StudentRecordJournalDTO convertJournalDTO(StudentRecordJournalFinalDTO studentRecordJournalFinal, SettlementResult settlementResult) {
        StudentRecordJournalDTO journal = new StudentRecordJournalDTO();
        journal.setJournalType(ReferenceTypeEnum.FINAL);
        journal.setJournalId(studentRecordJournalFinal.getJournalId());
        journal.setJournalLineId(studentRecordJournalFinal.getJournalLineId());
        journal.setOriginClazzId(studentRecordJournalFinal.getClazzId());
        journal.setOriginClazzName(studentRecordJournalFinal.getClazzName());
        journal.setOriginClazzOutDate(studentRecordJournalFinal.getSettlementDate());
        journal.setOriginClazzSettlementAmount(settlementResult.getSettlementAmount());
        journal.setOriginClazzSettlementDays(settlementResult.getSettlementDays());
        return journal;
    }
}
